package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.StatusConstant;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.SetmealService;
import com.sky.vo.DishItemVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 套餐service实现类
 */
@Service
public class SetmealServiceImpl implements SetmealService {
    @Autowired
    private SetmealMapper setmealMapper;

    @Autowired
    private SetmealDishMapper setmealDishMapper;

    /**
     * 套餐分页查询
     */
    @Override
    public PageResult pageQuery(SetmealPageQueryDTO setmealPageQueryDTO) {
        PageHelper.startPage(setmealPageQueryDTO.getPage(), setmealPageQueryDTO.getPageSize());
        Page<Setmeal> setmeals = setmealMapper.pageQuery(setmealPageQueryDTO);
        return new PageResult(setmeals.getTotal(), setmeals.getResult());
    }

    /**
     * 添加套餐
     */
    @Override
    public void add(SetmealDTO setmealDTO) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO, setmeal);
        setmeal.setStatus(1);
        setmealMapper.add(setmeal);
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        Long id = setmeal.getId();
        if (setmealDishes != null && setmealDishes.size() > 0) {
            for (SetmealDish setmealDish : setmealDishes) {
                setmealDish.setSetmealId(id);
            }
            setmealDishMapper.add(setmealDishes);
        }
    }

    /**
     * 根据id获取套餐
     */
    @Override
    public Setmeal getById(Long id) {
        return setmealMapper.getById(id);
    }

    /**
     * 修改套餐状态
     */
    @Override
    public void startOrStop(Integer status, Long id) {
        Setmeal setmeal = Setmeal.
                builder().
                id(id).
                status(status).
                build();
        setmealMapper.startOrStop(setmeal);
    }

    /**
     * 批量删除套餐
     */
    @Override
    public void batchDelete(List<Long> ids) {
        if (ids != null) {
            setmealDishMapper.batchDelete(ids);
            setmealMapper.batchDelete(ids);
        }
    }

    /**
     * 修改套餐
     */
    @Override
    public void update(SetmealDTO setmealDTO) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO, setmeal);
        Long id = setmealDTO.getId();
        setmealMapper.update(setmeal);
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        if (setmealDishes != null && setmealDishes.size() > 0) {
            for (SetmealDish setmealDish : setmealDishes) {
                setmealDish.setSetmealId(id);
            }
            setmealDishMapper.delete(id);
            setmealDishMapper.add(setmealDishes);
        }
    }

    /**
     * 根据分类id查询套餐
     */
    @Override
    public List<Setmeal> userGetSetmealsByCategoryId(Long categoryId) {
        return setmealMapper.userGetSetmealsByCategoryId(categoryId);
    }

    /**
     * 根据套餐id查询包含的菜品
     */
    @Override
    public List<DishItemVO> userGetDishesBySetmealId(Long id) {
        return setmealDishMapper.userGetDishesBySetmealId(id);
    }
}